package com.venta.autopartes.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.venta.autopartes.branch.Branch;
import com.venta.autopartes.branch.BranchDepartment;
import com.venta.autopartes.branch.BranchUserRole;
import com.venta.autopartes.car.Address;
import com.venta.autopartes.car.Color;
import com.venta.autopartes.car.CommonUsage;
import com.venta.autopartes.car.Country;
import com.venta.autopartes.car.Currency;
import com.venta.autopartes.car.Equipment;
import com.venta.autopartes.car.Location;
import com.venta.autopartes.car.MakeVehicle;
import com.venta.autopartes.car.ModelVehicle;
import com.venta.autopartes.car.PaymentMethod;
import com.venta.autopartes.car.PaymentType;
import com.venta.autopartes.car.SeatMaterial;
import com.venta.autopartes.car.State;
import com.venta.autopartes.car.Suburb;
import com.venta.autopartes.car.Town;
import com.venta.autopartes.car.SubModelVehicle;
import com.venta.autopartes.car.TransmissionType;
import com.venta.autopartes.car.VehicleCharacteristic;
import com.venta.autopartes.car.VehicleCharacteristicProperty;
import com.venta.autopartes.dao.BranchDAO;
import com.venta.autopartes.dao.CurrencyDAO;
import com.venta.autopartes.dao.EquipmentDAO;
import com.venta.autopartes.dao.LocationDAO;
import com.venta.autopartes.dao.PaymentDAO;
import com.venta.autopartes.dao.UserDAO;
import com.venta.autopartes.dao.UtilitiesDAO;
import com.venta.autopartes.dao.VehicleDAO;
import com.venta.autopartes.user.Gender;
import com.venta.autopartes.user.Phone;
import com.venta.autopartes.user.Question;
import com.venta.autopartes.user.User;
import com.venta.autopartes.user.UserPrivilage;

public class DataCatalog {

	private boolean filled=false;
	private List<MakeVehicle> makeVehicleList=new ArrayList<MakeVehicle>();
	private List<ModelVehicle> modelVehicleList=new ArrayList<ModelVehicle>();
	private List<SubModelVehicle> subModelVehicleList=new ArrayList<SubModelVehicle>();
	private List<String> yearVehicleList=new ArrayList<String>();
	
	private List<Currency> currencyList= new ArrayList<Currency>();
	private List<PaymentMethod> paymentMethodList= new ArrayList<PaymentMethod>();
	private List<PaymentType> paymentTypeList= new ArrayList<PaymentType>();
	private List<CommonUsage> commonUsageList= new ArrayList<CommonUsage>();
	private List<Color> colorList= new ArrayList<Color>();
	private List<SeatMaterial> seatMaterialList= new ArrayList<SeatMaterial>();
	private List<TransmissionType> transmisionTypeList= new ArrayList<TransmissionType>();
	
	private Location locations = new Location();
	
	private Equipment equipment=new Equipment();;
	private List<String> specialOptionsAttributesList= new ArrayList<String>();
	
	private List<BranchDepartment> branchDepartmentsList = new ArrayList<BranchDepartment>();
	private List<BranchUserRole> branchUserRoleList= new ArrayList<BranchUserRole>();
	
	private List<Phone> phoneList= new ArrayList<Phone>();
	private List<Gender> genderList=new ArrayList<Gender>();
	private List<UserPrivilage> userPrivilageList=new ArrayList<UserPrivilage>();
	private List<Question> questionList=new ArrayList<Question>();
	
	private List<User> branchManagersList=new ArrayList<User>();
	private List<Branch> branchMatrixList=new ArrayList<Branch>();
	
	
	public DataCatalog(){
		if(this.isFilled()==false){
			
			System.out.println("Catalog List will be filled from Database ... ");
			
			currencyList=CurrencyDAO.getCurrencyList();
			System.out.println("Currency List " + currencyList);
			paymentMethodList=PaymentDAO.getPaymentMethodList();
			paymentTypeList=PaymentDAO.getPaymentTypeList();
			colorList=UtilitiesDAO.getColorsList();
			seatMaterialList=VehicleDAO.getSeatMaterialList();
			
			makeVehicleList=new VehicleDAO().getAllMakeVehicleList();
			commonUsageList=VehicleDAO.getCommonUsageList();
			
			transmisionTypeList=VehicleDAO.getTransmissionTypeList();
			
			
			locations.setCountriesList(LocationDAO.getCountriesList());
			phoneList=LocationDAO.getPhoneList();
			genderList=UserDAO.getGenderList();
			userPrivilageList=UserDAO.getUserPrivilageList();
			questionList=UserDAO.getQuestionList();
			branchDepartmentsList=BranchDAO.getBranchDepartmentsList();
			branchUserRoleList=BranchDAO.getBranchUserRoleList();
			
			equipment.setCharacteristicsList(EquipmentDAO.getVehicleCharacteristicList());
			
			
			specialOptionsAttributesList=EquipmentDAO.getSpecialOptionsAttributesList();
			
			this.filled=true;
		}
	}
	
	public List<MakeVehicle> getMakeVehicleList() {
		return makeVehicleList;
	}
	public void setMakeVehicleList(List<MakeVehicle> makeVehicleList) {
		this.makeVehicleList = makeVehicleList;
	}
	public List<ModelVehicle> getModelVehicleList() {
		return modelVehicleList;
	}
	public void setModelVehicleList(List<ModelVehicle> modelVehicleList) {
		this.modelVehicleList = modelVehicleList;
	}
	public List<SubModelVehicle> getSubModelVehicleList() {
		return subModelVehicleList;
	}
	public void setSubModelVehicleList(List<SubModelVehicle> subModelVehicleList) {
		this.subModelVehicleList = subModelVehicleList;
	}
	public List<String> getYearVehicleList() {
		return yearVehicleList;
	}
	public void setYearVehicleList(List<String> yearVehicleList) {
		this.yearVehicleList = yearVehicleList;
	}
	
	public List<Currency> getCurrencyList() {
		return currencyList;
	}

	public void setCurrencyList(List<Currency> currencyList) {
		this.currencyList = currencyList;
	}
	
	public List<PaymentMethod> getPaymentMethodList() {
		return paymentMethodList;
	}

	public void setPaymentMethodList(List<PaymentMethod> paymentMethodList) {
		this.paymentMethodList = paymentMethodList;
	}

	public List<CommonUsage> getCommonUsageList() {
		return commonUsageList;
	}

	public void setCommonUsageList(List<CommonUsage> commonUsageList) {
		this.commonUsageList = commonUsageList;
	}

	public List<Color> getColorList() {
		return colorList;
	}

	public void setColorList(List<Color> colorList) {
		this.colorList = colorList;
	}

	public List<SeatMaterial> getSeatMaterialList() {
		return seatMaterialList;
	}

	public void setSeatMaterialList(List<SeatMaterial> seatMaterialList) {
		this.seatMaterialList = seatMaterialList;
	}

	public List<TransmissionType> getTransmisionTypeList() {
		return transmisionTypeList;
	}

	public void setTransmisionTypeList(List<TransmissionType> transmisionTypeList) {
		this.transmisionTypeList = transmisionTypeList;
	}

	public Location getLocations() {
		return locations;
	}

	public void setLocations(Location locations) {
		this.locations = locations;
	}
	
	
	public Equipment getEquipment() {
		return equipment;
	}

	public void setEquipment(Equipment equipment) {
		this.equipment = equipment;
	}

	public List<String> getSpecialOptionsAttributesList() {
		return specialOptionsAttributesList;
	}
	public void setSpecialOptionsAttributesList(
			List<String> specialOptionsAttributesList) {
		this.specialOptionsAttributesList = specialOptionsAttributesList;
	}

	public boolean isFilled() {
		return filled;
	}

	public void setFilled(boolean filled) {
		this.filled = filled;
	}

	public List<PaymentType> getPaymentTypeList() {
		return paymentTypeList;
	}

	public void setPaymentTypeList(List<PaymentType> paymentTypeList) {
		this.paymentTypeList = paymentTypeList;
	}

	public List<BranchDepartment> getBranchDepartmentsList() {
		return branchDepartmentsList;
	}

	public void setBranchDepartmentsList(
			List<BranchDepartment> branchDepartmentsList) {
		this.branchDepartmentsList = branchDepartmentsList;
	}

	public List<BranchUserRole> getBranchUserRoleList() {
		return branchUserRoleList;
	}

	public void setBranchUserRoleList(List<BranchUserRole> branchUserRoleList) {
		this.branchUserRoleList = branchUserRoleList;
	}

	public List<Phone> getPhoneList() {
		return phoneList;
	}

	public void setPhoneList(List<Phone> phoneList) {
		this.phoneList = phoneList;
	}

	public List<Gender> getGenderList() {
		return genderList;
	}

	public void setGenderList(List<Gender> genderList) {
		this.genderList = genderList;
	}

	public List<UserPrivilage> getUserPrivilageList() {
		return userPrivilageList;
	}

	public void setUserPrivilageList(List<UserPrivilage> userPrivilageList) {
		this.userPrivilageList = userPrivilageList;
	}

	public List<Question> getQuestionList() {
		return questionList;
	}

	public void setQuestionList(List<Question> questionList) {
		this.questionList = questionList;
	}
	
	public List<User> getBranchManagersList() {
		return branchManagersList;
	}

	public void setBranchManagersList(List<User> branchManagersList) {
		this.branchManagersList = branchManagersList;
	}

	public List<Branch> getBranchMatrixList() {
		return branchMatrixList;
	}

	public void setBranchMatrixList(List<Branch> branchMatrixList) {
		this.branchMatrixList = branchMatrixList;
	}

	public Address getAddressNamesByCountryId(Address selectedAddress){
		for(Country country:getLocations().getCountriesList()){
			if(country.getCountryId()==selectedAddress.getCountry().getCountryId()){
				selectedAddress.getCountry().setCode(country.getCode());
				selectedAddress.getCountry().setCountry(country.getCountry());
				selectedAddress.getCountry().setStates(country.getStates());
			}
		}
		
		for(State state:selectedAddress.getCountry().getStates()){
			if(state.getStateId()==selectedAddress.getState().getStateId()){
				selectedAddress.getState().setStateId(state.getStateId());
				selectedAddress.getState().setState(state.getState());
				selectedAddress.getState().setTowns(state.getTowns());
				selectedAddress.getState().setCountryId(state.getCountryId());
			}
		}
		
		for(Town town:selectedAddress.getState().getTowns()){
			if(town.getTownId()==selectedAddress.getTown().getTownId()){
				selectedAddress.getTown().setTownId(town.getTownId());
				selectedAddress.getTown().setTown(town.getTown());
				selectedAddress.getTown().setSuburbs(town.getSuburbs());
				selectedAddress.getTown().setStateId(town.getStateId());
			}
		}
		
		for(Suburb suburb:selectedAddress.getTown().getSuburbs()){
			if(suburb.getSuburbId()==selectedAddress.getSuburb().getSuburbId()){
				selectedAddress.getSuburb().setSuburbId(suburb.getSuburbId());
				selectedAddress.getSuburb().setSuburb(suburb.getSuburb());
				selectedAddress.getSuburb().setZipcode(suburb.getZipcode());
				selectedAddress.setZip(suburb.getZipcode());
			}
		}
	
		return selectedAddress;
	}

	
	public Gender getGenderByGenderId(Gender selectedGender){
		for(Gender gender:getGenderList()){
			if(gender.getGenderId()==selectedGender.getGenderId()){
				selectedGender.setGenderId(gender.getGenderId());
				selectedGender.setGender(gender.getGender());
			}
		}
		return selectedGender;
	}
	
	public UserPrivilage getUserPrivilageByUserPrivilageId(UserPrivilage selectedUserPrivilage){
		
		for(UserPrivilage userPrivilage:getUserPrivilageList()){
			if(userPrivilage.getUserPrivilageId()==selectedUserPrivilage.getUserPrivilageId()){
				selectedUserPrivilage.setUserPrivilageId(userPrivilage.getUserPrivilageId());
				selectedUserPrivilage.setUserPrivilage(userPrivilage.getUserPrivilage());
			}
		}
		return selectedUserPrivilage;
		
	}
	
	public UserPrivilage getUserPrivilageByUserPrivilageName(UserPrivilage selectedUserPrivilage){
		
		for(UserPrivilage userPrivilage:getUserPrivilageList()){
			if(userPrivilage.getUserPrivilage().equalsIgnoreCase(selectedUserPrivilage.getUserPrivilage())){
				selectedUserPrivilage.setUserPrivilageId(userPrivilage.getUserPrivilageId());
				selectedUserPrivilage.setUserPrivilage(userPrivilage.getUserPrivilage());
			}
		}
		return selectedUserPrivilage;
		
	}
	
	public UserPrivilage getUserPrivilageByUserPrivilageName(String selectedUserPrivilage){
		UserPrivilage foundUserPrivilage=null;
		for(UserPrivilage userPrivilage:getUserPrivilageList()){
			if(userPrivilage.getUserPrivilage().equalsIgnoreCase(selectedUserPrivilage)){
				foundUserPrivilage=userPrivilage;
			}
		}
		return foundUserPrivilage;
		
	}
	
	public BranchDepartment getBranchDepartmentByBranchDepartmentId( BranchDepartment selectedBranchDepartment){
		for(BranchDepartment branchDepartment: getBranchDepartmentsList()){
			if(branchDepartment.getBranchDepartmentId()==selectedBranchDepartment.getBranchDepartmentId()){
				selectedBranchDepartment.setBranchDepartmentId(branchDepartment.getBranchDepartmentId());
				selectedBranchDepartment.setBranchDepartment(branchDepartment.getBranchDepartment());
			}
		}
		return selectedBranchDepartment;
	}
	
	public BranchUserRole getBranchUserRoleByBranchUserRoleId(BranchUserRole selectedBranchUserRole){
		for(BranchUserRole branchUserRole:getBranchUserRoleList()){
			if(branchUserRole.getBranchUserRoleId()==selectedBranchUserRole.getBranchUserRoleId()){
				selectedBranchUserRole.setBranchUserRoleId(branchUserRole.getBranchUserRoleId());
				selectedBranchUserRole.setBranchUserRole(branchUserRole.getBranchUserRole());
			}
		}
		return selectedBranchUserRole;
	}
	
	public Question getQuestionByQuestionId(Question selectedQuestion){
		for(Question question:getQuestionList()){
			if(question.getQuestionId()==selectedQuestion.getQuestionId()){
				selectedQuestion.setQuestionId(question.getQuestionId());
				selectedQuestion.setQuestion(question.getQuestion());
			}
		}
		return selectedQuestion;
	}
	
	public Phone getPhoneByTypeId(Phone selectedPhone){
		for(Phone phone: getPhoneList()){
			if(phone.getTypeId()==selectedPhone.getTypeId()){
				selectedPhone.setTypeId(phone.getTypeId());
				selectedPhone.setPhoneType(phone.getPhoneType());
			}
		}
		return selectedPhone;
	}
	
	public List<Phone> getPhoneListByTypeId(List<Phone> selectedPhoneList){
		for(Phone phone:selectedPhoneList){
			getPhoneByTypeId(phone);
		}
		return selectedPhoneList;
	}
	
	public List<VehicleCharacteristicProperty> getVehicleCharacteristic(String selectedEquipment){
		List<VehicleCharacteristicProperty> selectedVehicleCharacteristicProperty= new ArrayList<VehicleCharacteristicProperty>();
		HashMap<Integer,VehicleCharacteristicProperty> vehicleCharacteristicPropertyHashMap= new HashMap<Integer,VehicleCharacteristicProperty>();
		for (VehicleCharacteristic vehicleCharacteristic:getEquipment().getCharacteristicsList()){
			for(VehicleCharacteristicProperty vehicleCharacteristicProperty:vehicleCharacteristic.getPropertiesList()){
				vehicleCharacteristicPropertyHashMap.put(vehicleCharacteristicProperty.getPropertyId(), vehicleCharacteristicProperty);
			}
		}
		
		String[] selectedEquipmentArray=selectedEquipment.split(",");
		
		for (String selectedEquipmentEntry:selectedEquipmentArray){
			selectedVehicleCharacteristicProperty.add(vehicleCharacteristicPropertyHashMap.get(selectedEquipmentEntry));
		}
		
		return selectedVehicleCharacteristicProperty;
		
	}
	
	public List<VehicleCharacteristic> getVehicleCharacteristicListBySelectedProperties(String vehicleCharacteristicProperties){
		List<VehicleCharacteristic> vehicleCharacteristicList= new ArrayList<VehicleCharacteristic>();
		HashMap<Integer,VehicleCharacteristic> vehicleCharacteristicHashMap= new HashMap<Integer,VehicleCharacteristic>();
		
		String[] vehicleCharacteristicPropertyArray=vehicleCharacteristicProperties.split(",");
		
		for(String vehicleCharacteristicPropertyString:vehicleCharacteristicPropertyArray ){
			VehicleCharacteristic vehicleCharacteristicCatalog=getVehicleCharacteristicByCharacteristicId(Integer.parseInt(vehicleCharacteristicPropertyString.trim()));
			VehicleCharacteristic vehicleCharacteristic=null;
			if (vehicleCharacteristicHashMap.get(vehicleCharacteristicPropertyString)==null){
				vehicleCharacteristic=new VehicleCharacteristic(Integer.getInteger(vehicleCharacteristicPropertyString),vehicleCharacteristicCatalog.getCharacteristic());
			}else{
				vehicleCharacteristic=vehicleCharacteristicHashMap.get(vehicleCharacteristicPropertyString);
				
			}
			vehicleCharacteristic.getPropertiesList().add(getVehicleCharacteristicPropertyById(Integer.parseInt(vehicleCharacteristicPropertyString)));
			vehicleCharacteristicList.add(vehicleCharacteristic);
		}
		
		return vehicleCharacteristicList;
		
	}
	
	public VehicleCharacteristicProperty getVehicleCharacteristicPropertyById(int vehicleCharacteristicPropertyId){
		VehicleCharacteristicProperty selectedVehicleCharacteristicProperty= null;
		
		for (VehicleCharacteristic vehicleCharacteristic:getEquipment().getCharacteristicsList()){
			for (VehicleCharacteristicProperty vehicleCharacteristicProperty: vehicleCharacteristic.getPropertiesList()){
				if (vehicleCharacteristicProperty.getPropertyId()==vehicleCharacteristicPropertyId){
					selectedVehicleCharacteristicProperty=vehicleCharacteristicProperty;
				}
			}
		}
		
		return selectedVehicleCharacteristicProperty;
	}
	
	public List<VehicleCharacteristic> getVehicleCharacteristicListBySelectedProperties(List<VehicleCharacteristicProperty> vehicleCharacteristicPropertyList){
		List<VehicleCharacteristic> vehicleCharacteristicList= new ArrayList<VehicleCharacteristic>();
		HashMap<Integer,VehicleCharacteristic> vehicleCharacteristicHashMap= new HashMap<Integer,VehicleCharacteristic>();
		
		for(VehicleCharacteristicProperty vehicleCharacteristicProperty:vehicleCharacteristicPropertyList ){
			VehicleCharacteristic vehicleCharacteristicCatalog=getVehicleCharacteristicByCharacteristicId(vehicleCharacteristicProperty.getCharacteristicId());
			VehicleCharacteristic vehicleCharacteristic=null;
			if (vehicleCharacteristicHashMap.get(vehicleCharacteristicProperty.getCharacteristicId())==null){
				vehicleCharacteristic=new VehicleCharacteristic(vehicleCharacteristicCatalog.getCharacteristicId(),vehicleCharacteristicCatalog.getCharacteristic());
			}else{
				vehicleCharacteristic=vehicleCharacteristicHashMap.get(vehicleCharacteristicProperty.getCharacteristicId());
				
			}
			vehicleCharacteristic.getPropertiesList().add(vehicleCharacteristicProperty);
			vehicleCharacteristicList.add(vehicleCharacteristic);
		}
		
		return vehicleCharacteristicList;
		
	}
	
	public VehicleCharacteristic getVehicleCharacteristicByCharacteristicId(int characteristicId){
		VehicleCharacteristic selectedVehicleCharacteristic=null;
		
		for(VehicleCharacteristic vehicleCharacteristic:getEquipment().getCharacteristicsList()){
			if(vehicleCharacteristic.getCharacteristicId()==characteristicId){
				selectedVehicleCharacteristic=vehicleCharacteristic;
			}
		}
		
		return selectedVehicleCharacteristic;

	}

	
	
}
